package com.atguigu.system.service.impl;

import com.atguigu.admin.utils.DataUtil;
import com.atguigu.system.mapper.ProductInfoFlowMapper;
import com.atguigu.system.model.entity.ProductInfoFlow;
import com.atguigu.system.model.entity.Sku;
import com.atguigu.system.model.entity.Spu;
import com.atguigu.system.service.ProductInfoFlowService;
import com.atguigu.system.service.SkuService;
import com.atguigu.system.service.SpuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-03-01
 */
@Service
public class ProductInfoFlowServiceImpl extends ServiceImpl<ProductInfoFlowMapper, ProductInfoFlow> implements ProductInfoFlowService {

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuService spuService;

    @Override
    public Page<ProductInfoFlow> pageSpusByName(Page<ProductInfoFlow> page, String name, String type) {
        LambdaQueryWrapper<ProductInfoFlow> queryWrapper = new LambdaQueryWrapper<>();
        if (DataUtil.isNotEmpty(type)){
            queryWrapper.eq(ProductInfoFlow::getObjectType, type);
        }

        if (DataUtil.isNotEmpty(name)){
            queryWrapper.eq(ProductInfoFlow::getOperatorName, name);
        }

        Page<ProductInfoFlow> productInfoFlowPage = this.getBaseMapper().selectPage(page, queryWrapper);
        List<ProductInfoFlow> records = productInfoFlowPage.getRecords();
        records.forEach(i->{
            String objectType = i.getObjectType();
            String desc = null;
            if (objectType.equals("SPU")){

                Spu spu = spuService.selectByID(i.getObjectId());
                String spuName = spu.getSpuName();
                String brand = spu.getBrand();
                desc = spuName+"-"+brand;
            }
            if (objectType.equals("SKU")){
                Sku byId = skuService.selectByID(i.getObjectId());
                Spu spu = spuService.selectByID(byId.getSpuId());

                String spuName = spu.getSpuName();
                String brand = spu.getBrand();
                desc = spuName+"-"+brand+"-"+byId.getColor()+"-"+byId.getSize()+"-"+byId.getQuality();
            }
            i.setDesc(desc);
        });
        productInfoFlowPage.setRecords(records);

        return productInfoFlowPage;
    }
}
